package com.paper.tiger.easy.rpc.s.client.execute;

import com.paper.tiger.easy.rpc.annotation.AsyncCall;
import com.paper.tiger.easy.rpc.s.client.SpringContextSupport;
import com.paper.tiger.easy.rpc.s.client.client.SyncUtil;
import com.paper.tiger.easy.rpc.s.client.common.Dove;
import io.netty.channel.ChannelHandlerContext;

/**
 * [简要描述]: 调用客户端，收到的数据返回
 * [详细描述]:
 *
 * @author ZouHaiBo
 * @version 1.0, 2021/6/17 20:35
 * @since JDK 1.8
 */
public class ResultFlyDove implements FlyDove
{

    /**
     * 客户端收到服务器端返回的数据
     * @param msg
     * @param ctx
     */
    @Override
    public void service(Dove msg, ChannelHandlerContext ctx)
    {
        try
        {
            // 如果是同步调用，放入同步队列
            if (msg.getSync()){
                SyncUtil.putData(msg.getDoveId(),msg);
            }else {
                // 异步调用
                AsyncCall bean = SpringContextSupport.getBean(msg.getAsyncClass());

                if (bean == null){
                    // todo 这里如果没有对象，需要new 玩之后进行缓存
                    AsyncCall asyncAccept = msg.getAsyncClass().newInstance();
                    asyncAccept.call(msg.getResult(),msg.getE());
                }else {
                    // 走spring的调用
                    bean.call(msg.getResult(),msg.getE());
                }

            }

        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
